
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Text;
using System.Xml;
using Swaf;
using Swaf.BizRule;
using Swaf.Call;

namespace Swaf.BizRule
{
    public class DefaultDynamicRuleRegistration
    {
        /// <summary>
        /// This is a ClassFactory that is used to handle the contents of a catalog item.  It will create the
        /// specified call type based on the provided BusinessRule definition.
        /// </summary>
        /// <param name="req">The contents of the catalog item.</param>
        /// <param name="catalogName">The name used to look up the catalog item.</param>
        /// <returns>A BasicRuleInfo that the RuleManager knows how to operate on.</returns>
        public static BasicRuleInfo createInstance(XmlElement req, string catalogName)
        {
            //----------------------------------------
            // Convert definition to a BasicRuleInfo 
            //----------------------------------------
            ICall callinfo = (ICall) Application.currentApp.factories["CallTypes"].createFromNickName(req.ChildNodes[0].Name, req.GetAttribute("name"), req.ChildNodes[0]);
            BasicRuleInfo info = new BasicRuleInfo(callinfo, catalogName, CallDistanceType.inDomain);

            //--------------------------------------------------------------
            // create all decorators specified on the rule's xml definition
            //--------------------------------------------------------------
            IClassFactory decoratorFactory = Application.currentApp.factories["BizRuleDecorators"];
            XmlElement parent = req.OwnerDocument.DocumentElement;
            for (int i = 0; i < parent.Attributes.Count; i++)
            {
                string decoratorName = parent.Attributes[i].Name;
                string decoratorValue = parent.Attributes[i].InnerText;
                if (decoratorFactory.entries.Contains(decoratorName))
                {
                    IDecorator decorator = (IDecorator)decoratorFactory.createFromNickName(decoratorName, decoratorName, decoratorValue, null);
                    info.addDecorator(decorator);
                }
            }
            
            // Set the expiration if it was specified
            info.expiration = req.GetAttribute("expire");
            
            // Set the expiration
            if (info.expiration == string.Empty)
            {
                // Get the default expiration if it is specified
                string boExpiration = Application.currentApp.resMgr.resolve("$DynamicBizRuleExpiration$");
                if (boExpiration == null || boExpiration == string.Empty || boExpiration[0] == '$')
                {
                    // No expiration by default
                    boExpiration = string.Empty;
                }

                info.expiration = boExpiration;
            }

            return info;
        }

        /// <summary>
        /// The ICall implementation is used to dynamically define a business rule that is found in the
        /// BusinessRules catalog.  Since it is using a Catalog, the Catalog itself will provide any caching
        /// that is necessary for the result.
        /// </summary>
        /// <param name="ruleName">The name of the rule that is to be looked up.</param>
        /// <returns>The BasicRuleInfo that the was registered in the catalog.</returns>
        public BasicRuleInfo defineRule(string ruleName)
        {
            // Load the business rule using the BusinessRules catalog
            BasicRuleInfo ruleInfo = Application.currentApp.appData.get(@"BusinessRules." + ruleName) as BasicRuleInfo;

            return ruleInfo;
        }
    }
}
